package com.cqc.swipemenulistview01;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private SwipeMenuListView listView;
    private Context context = this;
    private ArrayList<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            list.add("item" + i);
        }
        listView = (SwipeMenuListView) findViewById(R.id.swipeMenuListView);

        MyAdapter adapter = new MyAdapter();
        listView.setAdapter(adapter);

        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {

                SwipeMenuItem item = new SwipeMenuItem(getApplicationContext());
                item.setTitle("收藏");
                item.setTitleSize(20);//必须设置
                item.setTitleColor(Color.RED);
                item.setBackground(R.color.colorPrimary);
                item.setWidth(100);//必须设置

                menu.addMenuItem(item);

                SwipeMenuItem item2 = new SwipeMenuItem(getApplicationContext());
                item2.setTitle("删除");
                item2.setTitleColor(Color.RED);
                item2.setTitleSize(18);
                item2.setBackground(R.color.colorPrimary);
                item2.setWidth(120);
                item2.setIcon(R.mipmap.ic_launcher_round);

                menu.addMenuItem(item2);
            }
        };
        listView.setMenuCreator(creator);


        listView.setCloseInterpolator(new BounceInterpolator());
        listView.setOpenInterpolator(new BounceInterpolator());

        listView.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);//向左滑，menu在右边

        listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                Log.d(TAG, position + "****" + index);//position就是adapter中的position，index是menuItem的索引，从0开始
                switch (index) {
                    case 0:
                        Toast.makeText(context, "收藏", Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        Toast.makeText(context, "删除", Toast.LENGTH_SHORT).show();
                        break;
                }
                //position=5时，点击menuItem后menu没有消失
                if (position == 5) {
                    return true;
                }
                return false;//点击menuItem后menu是否dismiss。true： 不消失，false:消失
            }
        });

        listView.setOnMenuStateChangeListener(new SwipeMenuListView.OnMenuStateChangeListener() {
            @Override
            public void onMenuOpen(int position) {
                Toast.makeText(context, "Menu打开了", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onMenuClose(int position) {
                Toast.makeText(context, "Menu关闭了", Toast.LENGTH_SHORT).show();
            }
        });

        listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

            }
        });
    }


    public class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
//              convertView = View.inflate(context, R.layout.item, null);//有错误
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tv.setText(list.get(position));
            return convertView;
        }
    }

    public static class ViewHolder {
        TextView tv;

        public ViewHolder(View itemView) {
            tv = (TextView) itemView.findViewById(R.id.tv1);
        }
    }


    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }
}
